﻿Option Strict On

Public Class Form1

    Private m_Display As Display
    Private m_StartVertex As Point?
    Private m_EndVertex As Point?

    Private Sub GraphPicture_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GraphPicture.MouseDown
        Select Case e.Button
            Case MouseButtons.Left
                m_StartVertex = m_Display.FindVertex(e.Location)
        End Select
    End Sub

    Private Sub GraphPicture_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GraphPicture.MouseMove
        Select Case e.Button
            Case MouseButtons.Left
                If m_StartVertex IsNot Nothing Then
                    m_EndVertex = e.Location
                    GraphPicture.Invalidate()
                End If
        End Select
    End Sub

    Private Sub GraphPicture_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GraphPicture.MouseUp
        Select Case e.Button
            Case MouseButtons.Left
                If m_StartVertex IsNot Nothing Then
                    Dim target = m_Display.FindVertex(e.Location)
                    If target IsNot Nothing Then
                        m_Display.AddEdge(m_StartVertex.Value, target.Value)
                    End If
                    m_StartVertex = Nothing
                    m_EndVertex = Nothing
                    GraphPicture.Invalidate()
                Else
                    m_Display.AddVertex(e.Location)
                End If
            Case MouseButtons.Right
                Dim p = m_Display.FindVertex(e.Location)
                If p IsNot Nothing Then
                    m_Display.RemoveVertex(p.Value)
                End If
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        m_Display = New Display(GraphPicture, DiffLabel)
        RSelector.Value = RToTick(1.1)
    End Sub

    Private Sub GraphPicture_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles GraphPicture.Paint
        m_Display.Draw(e)
        If m_StartVertex IsNot Nothing AndAlso m_EndVertex IsNot Nothing Then
            Using p As New Pen(Color.Green, 1)
                p.DashStyle = Drawing2D.DashStyle.Dash
                e.Graphics.DrawLine(p, m_StartVertex.Value, m_EndVertex.Value)
            End Using
        End If
    End Sub

    Private Sub RSelector_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RSelector.ValueChanged
        RLabel.Text = TickToR(RSelector.Value).ToString("0.0")
    End Sub

    Private Function TickToR(ByVal tick As Integer) As Double
        Return tick / 10
    End Function

    Private Function RToTick(ByVal r As Double) As Integer
        Return CInt(r * 10)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        m_Display.InitMatrix(TickToR(RSelector.Value), 0.00000000000001)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        m_Display.NextStep()
    End Sub
End Class
